﻿Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports Sy.UI
Imports Sy.UI.Cef

Public Class Form2
    Public WithEvents CefWeb As Sy.UI.CefBrowser
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Sy.Update.CheckVersion()
        Try
            If (UBound(Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName)) > 0) Then
                MessageBox.Show("当前程序已启动", "禁止运行 ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Close()
                End
            End If

            Me.Text = Version
            Label1.Text = Version
            If Version = "米境通僵尸无主Asin采集工具" Then
                PictureBox1.ImageLocation = "https://www.miwaimao.com/img/tool/jslogin.jpg"
            End If
            If Version = "米境通亚马逊选品工具" Then
                PictureBox1.ImageLocation = "https://www.miwaimao.com/img/tool/splogin.jpg"
            End If
            If Version = "米境通跨境电商图片批量处理工具" Then
                PictureBox1.ImageLocation = "https://www.miwaimao.com/img/tool/imglogin.jpg"
            End If

            If IO.File.Exists(Application.StartupPath & "\Config\user.info") Then
                Try
                    Dim str = IO.File.ReadAllText(Application.StartupPath & "\Config\user.info")
                    Dim nstr = Split(str, "|||")
                    If nstr(0) <> "" Then
                        Me.UserName.Text = nstr(0)
                        Me.Password.Text = nstr(1)
                        CheckBox1.Checked = True
                    End If
                Catch ex As Exception

                End Try
            End If
        Catch ex As Exception
            MsgBox("载入出错！")
        End Try
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Dim tcpClient = New TcpClient()
        'tcpClient.Connect(IPAddress.Parse("127.0.0.1"), 5589)
        'Dim ntwStream = tcpClient.GetStream()
        'If ntwStream.CanWrite Then
        '    Dim bytSend = Encoding.UTF8.GetBytes("aaaaaaaaaaa")
        '    ntwStream.Write(bytSend, 0, bytSend.Length)
        'Else
        '    ntwStream.Close()
        '    tcpClient.Close()
        'End If
        'ntwStream.Close()
        'tcpClient.Close()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        'Dim html2 = getnewhtml("https://www.miwaimao.com/testhead.php")
        'Console.WriteLine(html2)
        'Form4.Pnl_Web.Controls.Remove(CefWeb)
        'Dim url = "https://www.trademarkia.com/trademarks-search.aspx?tn=gucci"
        'CefWeb = Sy.UI.CefBrowser.LoadBrowser(Form4.Pnl_Web, url)
        'Dim page As String = ""
        'Do Until InStr(page, "txt txt-lg") > 0
        '    'page = getbrhtml(CefWeb)
        '    Application.DoEvents()
        'Loop
        'If InStr(page, "Here are your search results") > 0 Then
        '    Console.WriteLine("yes")
        'Else
        '    Console.WriteLine("no")
        'End If

    End Sub

    Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
        Try
            If CheckBox1.Checked Then
                Try
                    Dim str = Me.UserName.Text & "|||" & Me.Password.Text
                    IO.File.WriteAllText(Application.StartupPath & "\Config\user.info", str)
                Catch ex As Exception
                    MsgBox("记住密码失败！")
                End Try
            End If
            UserNamelo = Me.UserName.Text
            Userpasslo = Me.Password.Text
            Dim Password = Me.Password.Text
            Dim MachineCode = EnumNetworkAdapters()
            Dim ID = "123"
            logintime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")
            Console.WriteLine(logintime)
            Dim url = "https://www.miwaimao.com/softlogin.php?UserName=" & UserNamelo & "&Password=" & Password & "&MachineCode=" & MachineCode & "&logintime=" & logintime & "&Version=" & Version & "&ID=" & ID & "&step=1"
            Console.WriteLine(url)
            If Trim(UserNamelo) = "" Or Trim(Password) = "" Then
                MsgBox("账户名或密码不能为空！")
                Exit Sub
            End If
            Dim html = getnewhtml(url)
            Console.WriteLine(html)
            If html = "1||" Then
                MsgBox("账户或密码错误，请重新尝试！")
                Exit Sub
            End If
            If html = "3||" Then
                MsgBox("您的账户已被锁定，请联系客服！")
                Exit Sub
            End If
            If html = "4||" Then
                MsgBox("您的授权已到期或尚未开通授权，请联系客服！")
                Exit Sub
            End If
            If InStr(html, "7|") > 0 Then
                Me.Hide()
                If Version = "米境通僵尸无主Asin采集工具" Then
                    Form1.Show()
                    Form1.Timer1.Enabled = True
                ElseIf Version = "米境通亚马逊选品工具" Then
                    Form3.Show()
                    Form3.Timer1.Enabled = True
                ElseIf Version = "米境通跨境电商图片批量处理工具" Then
                    Form5.Show()
                    Form5.Timer1.Enabled = True
                ElseIf Version = "米境通百度统计分析工具" Then
                    Form6.Show()
                    Form6.Timer1.Enabled = True
                Else
                    End
                End If
            End If
        Catch ex As Exception
            MsgBox("登录失败！")
        End Try
    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim url = "https://www.miwaimao.com/"
        System.Diagnostics.Process.Start(url)
    End Sub


End Class